﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.Repositories;
using EntitiesLayer;
using DAL.AbstractRepositories;
using System.Data.SqlClient;
using System.Data;

namespace DAL.Repositorios
{
    public class CitasRepository : DAL.AbstractRepositories.AbstractRepository<Cita>
    {
        
        public override void Insert(Cita entity)
        {
            this.Inserted.Add(entity);
        }
        
        public override void Delete(Cita entity)
        {
            this.Deleted.Add(entity);
        }
        
        public override void Update(Cita entity)
        {
            this.Updated.Add(entity);
        }

        //implentar
        public override IEnumerable<Cita> GetAll()
        {
            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaListarCitas");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                yield return crearCita(dr, Convert.ToInt32(dr["IdCita"]));
            }
        }

        private Cita crearCita(DataRow dr, int p)
        {
          Cita objCita = new Cita( 
              Convert.ToDateTime(dr["FechaHoraCita"]),
              Convert.ToInt32(dr["idEncargado"]),
              Convert.ToInt32(dr["idCliente"]),
              Convert.ToString(dr["Notas"]),
              Convert.ToInt32(dr["idTipoCita"]),
              Convert.ToChar(dr["Estado"]),
              p );

           return objCita;
        }
        //implentar
        public override Cita GetById(int id)
        {
            Cita objCita = null;
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdCita", id));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaConsultarCitaPorID");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                objCita = crearCita(dr, id);
            }

            return objCita;
        }

        protected override void InsertEntity(Cita objCita)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@IdTipoCita", objCita.TipoCita ));
                cmd.Parameters.Add(new SqlParameter("@FechaHoraCita", objCita.FechaHora));
                cmd.Parameters.Add(new SqlParameter("@IdEncargado", objCita.IdEncargado));
                cmd.Parameters.Add(new SqlParameter("@IdCliente", objCita.IdCliente));
                cmd.Parameters.Add(new SqlParameter("@Notas", objCita.Notas));
                cmd.Parameters.Add(new SqlParameter("@Estado", objCita.Estado));
                DBAccess.ExecuteSPNonQuery(ref cmd, "PaRegistrarCita");
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }

        }

        protected override void UpdateEntity(Cita objCita)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdMaquina", objCita.Id));
            cmd.Parameters.Add(new SqlParameter("@IdTipoCita", objCita.TipoCita));
            cmd.Parameters.Add(new SqlParameter("@FechaHoraCita", objCita.FechaHora));
            cmd.Parameters.Add(new SqlParameter("@IdEncargado", objCita.IdEncargado));
            cmd.Parameters.Add(new SqlParameter("@IdCliente", objCita.IdCliente));
            cmd.Parameters.Add(new SqlParameter("@Notas", objCita.Notas));
            cmd.Parameters.Add(new SqlParameter("@Estado", objCita.Estado)); 
            DBAccess.ExecuteSPNonQuery(ref cmd, "PaModificarCita");
        }
        protected override void DeleteEntity(Cita objCita)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdCita", objCita.Id));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaEliminarCita");
        }
    }
}
